import EyouWatchPool from "../../db/basePool";

export default class loginModel {

    /**
     * 上传批次
     **/
    // 查询批次列表
    async getBatchList(params){
        let limit = "";
        if(params.page && params.limit){
            var pageNumber = (params.page-1)*params.limit;
            limit += " LIMIT "+pageNumber+","+params.limit+""
        }
        let search = "";
        if(params.name){
            search = " and name like '%"+params.name.trim()+"%'"
        }
        let sql="select * from tbl_stock_batch where status=1 "+search+" order by createAt desc"+limit;
        let res = await new EyouWatchPool().query(sql);
        return res;
    }
    // 查询列表数量
    async getBatchCount(params){
        let search = "";
        if(params.name){
            search = " and name like '%"+params.name.trim()+"%'"
        }
        let sql="select count(*) c from tbl_stock_batch where status=1"+search;
        let res:any = await new EyouWatchPool().query(sql);
        return res[0] ? res[0].c : 0;
    }
    // 编辑
    async editBatch(obj){
        let sql="update tbl_stock_batch set ? where id='"+obj.id+"'";
        let res = await new EyouWatchPool().insert(sql,obj);
        return res;
    }
    // 添加
    async saveBatch(obj){
        let sql="insert into tbl_stock_batch set ? ";
        let res = await new EyouWatchPool().insert(sql,obj);
        return res;
    }
    // 删除
    async delBatch(id){
        let sql="update tbl_stock_batch set status=0 where id='"+id+"'";
        let res = await new EyouWatchPool().query(sql);
        return res;
    }


    /**
     * 数据查询
     **/
    async getDataList(params){
        let search = "";
        if(params.kh){
            search = " and kh like '%"+params.kh.trim()+"%'"
        }
        if(params.lh){
            let lhList = params.lh.split(",");
            lhList = lhList.map(item=>{
                item = "'"+item+"'"
                return item
            })
            search = " and lh in ("+lhList.join(',')+")"
        }
        let sql="select * from tbl_stock_data where status=1 and batchid='"+params.batchid+"'"+search+" order by createAt desc";
        let res = await new EyouWatchPool().query(sql);
        return res;
    }
    // 添加
    async saveExcelData(obj){
        let sql="insert into tbl_stock_data set ? ";
        let res = await new EyouWatchPool().insert(sql,obj);
        return res;
    }
    // 删除
    async delBatchData(id){
        let sql="delete from tbl_stock_data where batchid='"+id+"'";
        let res = await new EyouWatchPool().query(sql);
        return res;
    }

} 
